package cc.android.testapp.xp.hooker;

import android.app.Application;

import cc.android.testapp.cfg.CFG;
import cc.android.testapp.cfg.ConfigHelper;
import cc.android.testapp.util.CLog;
import cc.android.testapp.xp.HookBase;
import cc.android.testapp.xp.HookModule;
import cc.android.testapp.xp.hooker.adblock.util.XHook;
import cc.commons.util.reflect.MethodUtil;
import de.robv.android.xposed.callbacks.XC_LoadPackage;

@HookModule(packages = "com.baidu.BaiduMap", name = "百度地图")
public class HookBaidumap extends HookBase implements ConfigHelper.IConfig {
    @CFG
    private String mAdViewInitMethod = "com.baidu.baidumaps.operation.splash.HomeSplashPresenter.onCreate";
    @CFG
    private String mAdViewEndMethod = "onPause";
    @CFG
    private String mOnResumeAdMethod = "com.baidu.baidumaps.operation.splash.HomeSplashPresenter.onStart";

    @Override
    protected void hookToApp(XC_LoadPackage.LoadPackageParam pParam, Application pApp) throws Throwable {
        ClassLoader tLoader = pApp.getClassLoader();
        try {
            XHook.with(tLoader).match(new HBMethod(mAdViewInitMethod)).hookBefore((param, obj, pArr) -> {
                MethodUtil.invokeDeclaredMethod(obj.getClass(), mAdViewEndMethod, obj);
                param.setResult(null);
            });
        } catch (Throwable exp) {
            CLog.log("Hook " + this.getModuleName() + "启动页广告时发生错误", exp);
        }
        try {
            XHook.with(tLoader).match(new HBMethod(mOnResumeAdMethod)).doNothing();
        } catch (Throwable exp) {
            CLog.log("Hook " + this.getModuleName() + "唤醒页广告时发生错误", exp);
        }

    }

    @Override
    public String getCfgNode() {
        return "baidumap";
    }
}
